package cn.phub.org.service.impl;

import cn.phub.basic.exception.MyException;
import cn.phub.basic.service.impl.BaseServiceImpl;
import cn.phub.basic.util.BaiduAuditUtils;
import cn.phub.basic.vo.ShopVo;
import cn.phub.org.domain.Employee;
import cn.phub.org.domain.Shop;
import cn.phub.org.domain.ShopAuditLog;
import cn.phub.org.mapper.EmployeeMapper;
import cn.phub.org.mapper.ShopAuditLogMapper;
import cn.phub.org.mapper.ShopMapper;
import cn.phub.org.service.IEmployeeService;
import cn.phub.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jijie
 * @since 2022-03-28
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;
    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //判断参数(店铺名)不能为空
        String name = shop.getName();

        if (StringUtils.isEmpty(name)){
            //使用自定义异常MyException
            throw new MyException("店铺名字不能为空!");
        }

        //非法校验 智能审核
        if (!BaiduAuditUtils.TextCensor(name)) {
            throw new MyException("请你自重!");
        }

        //判断店铺是否被注册
        Shop shopName = shopMapper.loadByName(name);
        if (shopName != null) {
            throw new MyException("该店铺已经被注册,请重新注册!");
        }

        //判断管理员是否被注册(1.注册了并绑定了店铺 2.注册了但没绑定店铺)
        Employee admin = shop.getAdmin();
        Employee empName = employeeMapper.loadByUserName(admin.getUsername());
        if (empName != null && empName.getShop_id() != null ) {
            throw new MyException("该管理员已经注册并拥有了店铺,请更换管理员进行绑定!");
        }
        if (empName == null) {//判断没有员工并新增
            //保存员工
            //employeeMapper.save(admin);不能直接调用别的业务实体类的mapper操作数据
            //应该将对应的实体类交给对应的service实现类去操作
            employeeService.add(admin);
            empName = admin;
        }
        //保存店铺并设置管理员
        //获取员工的ID并重新获取到shop.setAdmainID外键里面
        shop.setAdminId(empName.getId());
        shopMapper.save(shop);
        //保存数据(保存员工,保存店铺,修改员工shop_id)
        //获取shop表里的Id,员工调用set方法将Id重新获取并修改
        empName.setShop_id(shop.getId());
        employeeMapper.update(empName);
    }

    @Override
    public void pass(ShopAuditLog log) {
        //修改店铺状态待激活
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //保存审核日志
        log.setAuditId(324);
        shopAuditLogMapper.save(log);
        //发送激活邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1509370299@qq.com");
        message.setTo("1509370299@qq.com");
        message.setSubject("洗脚城激活请求");
        message.setText("您的店铺已经初步审核通过，请手动完成激活并使用。激活链接：http://localhost:8080/shop/audit/"+log.getShopId());
        javaMailSender.send(message);
    }

    @Override
    public void reject(ShopAuditLog log) {
        //修改店铺状态驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        log.setAuditId(325);
        shopAuditLogMapper.save(log);
        //发送激活邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1509370299@qq.com");
        message.setTo("1509370299@qq.com");
        message.setSubject("驳回，请修改店铺信息，再次提交审核");
        message.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+log.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+log.getId()+"'>点击修改资料</a>");
        javaMailSender.send(message);
    }

    @Override
    public List<ShopVo> loadStateStatistics() {
        return shopMapper.loadStateStatistics();
    }
}
